package 多线程3;

import java.util.concurrent.BlockingQueue;
import java.util.concurrent.LinkedBlockingDeque;

//借助BlockingQueue写一个简单的生产者消费者模型
public class TestDemo3 {
    public static void main(String[] args) {
        //使用一个BlockingQueue 作为交易场所
        BlockingQueue<Integer> queue = new LinkedBlockingDeque<>();

        //搞一个线程作为消费者
        Thread customer = new Thread(){
            @Override
            public void run() {
                while(true){
                    try {
                        Integer value = queue.take();
                        System.out.println("消费元素:"+value);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
            }
        };
        customer.start();

        //搞一个线程作为生产者
        Thread producer = new Thread(){
            @Override
            public void run() {
                for(int i = 1;i <= 10000;i++){
                    System.out.println("生产了一个元素:"+i);
                    try {
                        queue.put(i);
                        Thread.sleep(1000);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
            }
        };
        producer.start();

        try {
            customer.join();
            producer.join();
        } catch (InterruptedException e) {
            e.printStackTrace();
        }

    }

}
